package cn.zymall.user.tool;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import java.util.HashMap;

/**
 * @author : th
 * @ClassName: Tools
 * @Description : 工具栏
 * @date 2020年08月19日 17:31
 **/
public class Tools {


    /**
     * @author：th
     * @Description：登录注册短信验证码
     * @Param：手机号,模板编号
     * @return：
     **/
  public static String sms(String value, String templateCode){
      DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G7immxMBjGDjCX23kTD", "RbpKQuYNiM0MSfv2gCU6YRwYfkcpxZ");
      IAcsClient client = new DefaultAcsClient(profile);

      // 生成随机数
      String code = String.valueOf((int)((Math.random()*9+1)*100000));


      HashMap<Object, Object> map = new HashMap<>(16);
      map.put("code",code);

      CommonRequest request = new CommonRequest();
      request.setSysMethod(MethodType.POST);
      request.setSysDomain("dysmsapi.aliyuncs.com");
      request.setSysVersion("2017-05-25");
      request.setSysAction("SendSms");
      request.putQueryParameter("RegionId", "cn-hangzhou");
      request.putQueryParameter("PhoneNumbers", value);
      request.putQueryParameter("SignName", "左右商城");
      request.putQueryParameter("TemplateCode", templateCode);
      request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));

      try {
          CommonResponse response = client.getCommonResponse(request);
          System.out.println(response.getData());
      } catch (ServerException e) {
          e.printStackTrace();
      } catch (ClientException e) {
          e.printStackTrace();
      }

      return code;

  }

}
